package anonymouschat.service;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import anonymouschat.utils.Print;

/**
 * A GUI created by Eclipse's Visual Editor that facilitates understanding the
 * server state
 * 
 */
public class ServerGUI extends Thread {

	/**
	 * Create a new Server GUI with a handle to the server running it
	 * 
	 * @param s
	 */
	public ServerGUI(Server s) {
		serverHandle = s;
	}

	// a handle to the server that creates this gui
	private Server serverHandle = null;

	private Shell sShell = null; // @jve:decl-index=0:visual-constraint="10,10"

	private StyledText publishedUsers_styledText = null;

	private StyledText onlineUsers_styledText = null;

	private StyledText existingConnections_styledText = null;

	private Label publishedUsers_label = null;

	private Label OnlineUsers_label = null;

	private Label existingConnections_label = null;

	private Label alerts_label = null;

	private Text alerts_textArea = null;

	/**
	 * check if any of the boxes have changes
	 */
	private void redraw() {
		String prev = null;
		String next = null;
		prev = this.onlineUsers_styledText.getText();
		next = serverHandle.getOnlineUsers();
		try {

			if (!prev.equalsIgnoreCase(next.trim())) {
				this.onlineUsers_styledText.setText(next);
				this.onlineUsers_styledText.redraw();
			}
		} catch (Exception e) {
			Print.error("Error updating online users", e);
		}
		prev = this.publishedUsers_styledText.getText().trim().toLowerCase();
		next = serverHandle.getPublishedUsers();
		try {

			if (!prev.equalsIgnoreCase(next.trim())) {
				this.publishedUsers_styledText.setText(next);
				this.publishedUsers_styledText.redraw();
			}
		} catch (Exception e) {
			Print.error("Error updating locally published users", e);
		}

		prev = this.existingConnections_styledText.getText().trim()
				.toLowerCase();
		next = Server.getAllConnections();
		try {
			if (!prev.equalsIgnoreCase(next.trim())) {
				this.existingConnections_styledText.setText(next);
				this.existingConnections_styledText.redraw();
			}
		} catch (Exception e) {
			Print.error("Error updating connection information");
		}

		next = Server.getNewAlerts();
		if (next != null) {
			this.alerts_textArea.append(next);
			this.alerts_textArea.redraw();
		}

	}

	/**
	 * This method initializes sShell
	 */
	private void createSShell() {
		sShell = new Shell();
		sShell.setText("Server " + serverHandle.getServerName() + " GUI");
		sShell.setLayout(null);
		sShell.setSize(new Point(673, 652));
		publishedUsers_styledText = new StyledText(sShell, SWT.H_SCROLL
				| SWT.FULL_SELECTION | SWT.READ_ONLY);
		publishedUsers_styledText.setText("Published Users");
		publishedUsers_styledText.setDoubleClickEnabled(false);
		publishedUsers_styledText.setBounds(new Rectangle(5, 27, 317, 318));
		onlineUsers_styledText = new StyledText(sShell, SWT.READ_ONLY
				| SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
		onlineUsers_styledText.setText("Online Users");
		onlineUsers_styledText.setDoubleClickEnabled(false);
		onlineUsers_styledText.setBounds(new Rectangle(329, 27, 317, 318));
		existingConnections_styledText = new StyledText(sShell, SWT.V_SCROLL
				| SWT.H_SCROLL | SWT.READ_ONLY | SWT.FULL_SELECTION);
		existingConnections_styledText.setText("Existing Connections");
		existingConnections_styledText.setDoubleClickEnabled(false);
		existingConnections_styledText
				.setBounds(new Rectangle(5, 369, 641, 79));
		publishedUsers_label = new Label(sShell, SWT.NONE);
		publishedUsers_label.setBounds(new Rectangle(5, 2, 161, 20));
		publishedUsers_label.setText("Locally Published Users");
		OnlineUsers_label = new Label(sShell, SWT.NONE);
		OnlineUsers_label.setBounds(new Rectangle(329, 2, 92, 20));
		OnlineUsers_label.setText("Online Users");
		existingConnections_label = new Label(sShell, SWT.NONE);
		existingConnections_label.setBounds(new Rectangle(5, 347, 159, 20));
		existingConnections_label.setText("Existing Connections");
		existingConnections_label.setBounds(new Rectangle(5, 347, 151, 20));
		existingConnections_label.setText("Existing Routes");
		alerts_label = new Label(sShell, SWT.NONE);
		alerts_label.setBounds(new Rectangle(5, 452, 95, 20));
		alerts_label.setText("Server Alerts");
		alerts_textArea = new Text(sShell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		alerts_textArea.setBounds(new Rectangle(8, 478, 641, 124));
	}

	@Override
	public void run() {
		Display display = Display.getDefault();
		this.createSShell();
		this.sShell.open();
		while (!this.sShell.isDisposed()) {
			this.redraw();

			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
		serverHandle.setShutDown(true);
		System.exit(0);
	}

	public void addAlert(String alertMessage) {
		this.alerts_textArea.append(alertMessage);
	}
}
